package com.jic.member.controller.manage;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.member.api.rest.MemberAddressRestApi;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberUser;
import com.jic.member.entity.power.MememberRecipientInformation;
import com.jic.member.request.power.MemberRecipientInformationRequest;
import com.jic.member.response.power.MemberRecipientInformationResponse;
import com.jic.member.service.power.MememberRecipientInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zxl
 * @date: 2020-01-09
 * @remark: BBC接口 会员地址列表
 */
@RestController
@RequestMapping("/apiMyAddress")
@Api(value = "APi-会员地址",tags = "APi-会员地址")
public class ApiMemberAddressController implements MemberAddressRestApi {

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Autowired
    MememberRecipientInformationService mememberRecipientInformationService;

    @ApiOperation(value = "获取用户地址列表")
    @ResponseBody
    @PostMapping("/getAddressList")
    @Override
    public RestResult<PageResult<MemberRecipientInformationResponse>> getAddressList(MemberRecipientInformationRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        String userStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + form.getToken());
        if (StringUtils.isEmpty(userStr)){
            return RestResult.error("4000","登录失效");
        };
        MememberUser mMember = JSONArray.parseObject(userStr, MememberUser.class);
        Long userId =mMember.getId();
        MememberRecipientInformation memberAddress = BeanUtil.copy(form, MememberRecipientInformation.class);
        memberAddress.setMemberUserId(userId);
        PageResult<MememberRecipientInformation> result = mememberRecipientInformationService.queryPage(memberAddress, page);
        List<MemberRecipientInformationResponse> memberAddressResponseList = result.getRows().stream().map(temp -> {
            MemberRecipientInformationResponse memberAddressResponse = BeanUtil.copy(temp, MemberRecipientInformationResponse.class);
            return memberAddressResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MemberRecipientInformationResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberAddressResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "新增/修改用户地址列表")
    @ResponseBody
    @PostMapping("/updateAddress")
    @Override
    public RestResult<String> updateAddress(MemberRecipientInformationRequest memberAddressRestRequest) {
        MememberRecipientInformation addressRequest = BeanUtil.copy(memberAddressRestRequest, MememberRecipientInformation.class);
        RestResult result = mememberRecipientInformationService.updateInfo(addressRequest);
        return result ;
    }

    @ApiOperation(value = "新增/修改用户地址列表")
    @ResponseBody
    @PostMapping("/deleteAddress")
    @Override
    public RestResult<String> deleteAddress(MemberRecipientInformationRequest memberAddressRestRequest) {
        MememberRecipientInformation addressRequest = BeanUtil.copy(memberAddressRestRequest, MememberRecipientInformation.class);
        RestResult result = mememberRecipientInformationService.deleteInfo(addressRequest);
        return result ;
    }


}
